<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="loginForm.mobile" name="mobile" placeholder="请输入手机号" :rules='loginFormRules.mobile'
        type="number" maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="loginForm.code" name="code" placeholder="请输入验证码" :rules='loginFormRules.code' type="number"
        maxlength="6">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <!-- time；倒计时时长 -->
          <van-count-down :time="1000*60" format='ss s' v-if="isCountDownShow" @finish='isCountDownShow=false' />
          <van-button class="send-sms-btn" round size="mini" type="default" @click="onSendSms" native-type="button"
            v-else>
            发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginPage',
  data () {
    return {
      loginForm: {
        mobile: '18911111111',
        code: '246810'
      },
      // 表单校验规则
      loginFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: '手机号格式错误' }],
        code: [{ required: true, message: '验证码不能为空' }, { pattern: /^\d{6}$/, message: '验证码格式错误' }]

      },
      isCountDownShow: false // 倒计时是否显示

    }
  },
  methods: {
    // 表单提交 检验通过触发
    async onSubmit () {
      // 获取表单数据
      const user = this.loginForm
      // 表单验证
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 0// 持续时间默认2000毫秒 如果为0 则持续展示
      })
      try {
        const res = await login(user)
        console.log('登陆成功', res)
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登陆成功')
        this.$router.go(-1)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          console.log('手机号或验证码错误')
          this.$toast.fail('手机号或验证码错误')
        } else {
          // console.log('登陆失败', err)
          this.$toast.fail('登陆失败请稍后重试')
        }
      }
    },
    // 发送验证码
    async onSendSms () {
      // 1验证手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('验证通过')
      } catch (err) {
        return console.log('验证失败', err)
      }

      // 3请求发送验证码
      try {
        await sendSms(this.loginForm.mobile)
        // console.log('发送成功', res)
        // 展示倒计时
        this.isCountDownShow = true
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败 关闭倒计时
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁，请稍后重试')
        } else {
          //  console.log('发送失败', err)
          this.$toast('发送失败，请稍后重试')
        }
      }
    }
    // 提交表单
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 28px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
  /deep/.van-icon-cross {
    color: #fff;
  }
}
</style>
